package common;

import java.util.Observable;

/**
 * Class for limited logging purposes (server console window)
 * 
 * @author Lukasz
 *
 */

public class Logger extends Observable {
	
	private boolean enabled = true;
	private boolean logToConsole = false;
	private boolean logToString = false;
	private StringBuffer logs = new StringBuffer();
	
	public Logger() {
		
	}
	
	public Logger(boolean enabled) {
		this.enabled = enabled;
	}
	
	public Logger(boolean logToConsole, boolean logToString) {
		this.logToConsole = logToConsole;
		this.logToString = logToString;
	}
	
	public synchronized boolean getLogToConsole() {
		return logToConsole;
	}
	
	public synchronized void setLogToConsole(boolean logToConsole) {
		this.logToConsole = logToConsole;
	}
	
	public synchronized void setLogToString(boolean logToString) {
		this.logToString = logToString;
	}
	
	public synchronized boolean isEnabled() {
		return enabled;
	}
	
	public synchronized void enable() {
		enabled = true;
	}
	
	public synchronized void disable() {
		enabled = false;
	}
	
	public synchronized String getLogs() {
		return logs.toString();
	}
	
	public synchronized void log(String msg) {
		if (enabled == false)
			return;
		
		logString(msg);
		logConsole(msg);
	}
	
	private synchronized void logConsole(String msg) {
		if (enabled == false)
			return;
		
		if (logToConsole == true) {
			System.out.println(msg);
		}
	}
	
	private synchronized void logString(String msg) {
		if (enabled == false)
			return;
		
		if (logToString == true) {
			if (logs.toString().equals("") == false)
				logs.append("\n");
			
			logs.append(msg);
			
			setChanged();
			notifyObservers(this);
		}
	}
	
	public synchronized void clear() {
		logs = new StringBuffer();
		
		setChanged();
		notifyObservers(this);
	}
	
}
